<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <!--2.2 在`el-form`组件和`el-form-item`组件分别添加rules属性model属性和prop属性 ref-->
      <el-form class="reg" :rules="regRules" :model="regForm" ref="refReg" status-icon>
        <!-- 1.2 v-model 双向数据绑定-->
        <el-form-item prop="username">
            <el-input v-model="regForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
            <el-input v-model="regForm.password"></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
            <el-input v-model="regForm.repassword"></el-input>
        </el-form-item>
         <el-button type="primary" style="  width: 100%;" @click="submitForm">注册</el-button>
         <el-link>去登录</el-link>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Reg',
  data(){
    // 3.3自定义验证
     var samePwd = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.regForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
     }
 return   {
     //1.1 储存表单数据
    regForm:{
        username:'',
        password:'',
        repassword:''
    },
    //2.1 储存表单验证规则对象
    regRules:{
        // 3.1.必填项验证
        username:[ { required: true, message: '请输入用户名', trigger: 'blur' },
        // 3.2 正则验证
       { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '用户名必须是1-10的字母数字', trigger: 'blur' }
        ],
        password:[{ required: true, message: '请输入密码', trigger: 'blur' },
         { pattern: /^\S{6,15}$/, message: '密码必须是6~15位非空字符', trigger: 'blur' },
        ],
        repassword:[{ required: true, message: '请输入确认密码', trigger: 'blur' },
           { pattern: /^\S{6,15}$/, message: '确认密码必须是6~15位非空字符', trigger: 'blur' },
           //  3.3自定义验证  validator 调用自定义验证
             { validator: samePwd, trigger: 'blur' }
           ]
    }
 }
  },
  methods:{
    // 4.兜底校验
    submitForm(){
        this.$refs.refReg.validate( async (valid) => {
         if(!valid) return console.log('通过失败');
         console.log('验证通过');
        //  5.1 发送axios 
        const {data:res} = await this.$http.post('/api/reg',this.regForm)
       console.log(res);
        // 5.2 判断业务状态码
        // 失败
        if(res.code !== 0) return this.$message.error(res.message)
        // 成功
        this.$message.success(res.message)
        // 跳转
        this.$router.push('/login')
      
        });
    }
  }
}
</script>

<style lang="less" scoped>
.reg-container {
  background: url('../../assets/images/login_bg.jpg') center;
  background-size: cover;
  height: 100%;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

.reg{
    padding: 0 25px;
}

    .title-box {
      height: 60px;
      background: url('../../assets/images/login_title.png') center no-repeat;
    }
  }
}
</style>